let MS = require('./ms')
const setHeader = (config) => {
    let header = {
        'X-MS-ApplicationId': config.applicationId,
        'X-MS-REST-API-KEY': config.applicationKey,
        'X-MS-APP-ID': config.appid
    }
    return header
}

const uniUploadFiles = (files) => {
    return new Promise((resolve, reject) => {
        var promise = Promise.all(files.map((file, index) => {
            return new Promise((resolve, reject) => {
                uni.uploadFile({
                    url: MS._config.host + MS._config.parameters.FILE.UPLOAD_FILE.url,
                    filePath: file,
                    name: 'file',
                    header: setHeader(MS._config),
                    success:(res) => {
                        if (res.statusCode === 200) {
                            var data = JSON.parse(res.data)
                            if(data.success) {
                                resolve(data.data)
                            } else {
                                reject(data.message)
                            }
                        } else {
                            reject(res)
                        }
                    },
                    fail: err => {
                        console.log(err)
                        reject(err)
                    }
                })
            })
        }));
        promise.then(function(results) {
            resolve(results);
        }).catch(function(err) {
            reject.log(err);
        });
    })
}

module.exports = uniUploadFiles